<template>
     <div v-if="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" size="mini" label-width="120px">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="新闻标题" prop="title" required>
                        <el-input v-model="dataForm.title" :maxlength="1024" placeholder="标题" size="small"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="新闻副标题" prop="description">
                <el-input v-model="dataForm.description" :maxlength="1024" placeholder="标题" size="small"></el-input>
            </el-form-item>
            <el-form-item label="新闻封面图" prop="image">
                <el-upload
                    class="avatar-uploader"
                    action="/wx/sys/file/upload"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :headers="{token: $cookie.get('token')}"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="photo" :src="photo" class="avatar" width="192px;height:108px">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <tinymce-editor ref="editor" v-model="dataForm.content"></tinymce-editor>
        </el-form>
        <div class="margin-top text-right">
            <el-button @click="cancal()" size="small">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
        </div>
    </div>



</template>

<script>
import { mapState } from 'vuex'
export default {
    name:'article-add-or-update',
    components: {
        TinymceEditor: () => import("@/components/tinymce-editor"),
        tagsEditor: () => import("@/components/tags-editor")
    },
    props:{
        visible:{
            type:Boolean,
            default:false
        }
    },
    data() {
        return {
            dataForm: {
                id: "",
                type: '1',
                title: "",
                content: "",
                summary: "",
                tags: "",
                articleType:'2',
                image: "",
                status:1,
                description:''
            },
            dataRule: {
                type: [
                    { required: true, message: "文章类型不能为空", trigger: "blur" }
                ],
                title: [
                    { required: true, message: "标题不能为空", trigger: "blur" }
                ],
                category: [
                    { required: true, message: "分类不能为空", trigger: "blur" }
                ]
            },
            photo:''
        };
    },
    computed: mapState({
        ARTICLE_TYPES: state=>state.article.ARTICLE_TYPES
    }),
    methods: {
        init(id) {
            this.dataForm.id = id || "";
            this.$nextTick(() => {
                this.$refs["dataForm"].resetFields();
                if (id > 0) {
                    this.$http({
                        url: this.$http.adornUrl(`/animal/article/info/byId/${this.dataForm.id}`),
                        method: "get",
                        params: this.$http.adornParams()
                    }).then(({ data }) => {
                        if (data && data.code === 200) {
                            this.dataForm=data.article;
                            this.dataForm.type = data.article.type + "";
                        }
                    });
                }
            });
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs["dataForm"].validate(valid => {
                if (valid) {
                    this.$http({
                        url: this.$http.adornUrl(`/animal/article/save`),
                        method: "post",
                        data: this.$http.adornData(this.dataForm)
                    }).then(({ data }) => {
                        if (data && data.code === 200) {
                            this.$message({
                                message: "操作成功",
                                type: "success",
                                duration: 1500,
                                onClose: () => {
                                    this.$emit("refreshDataList");
                                    this.$emit('hide')
                                }
                            });
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                }
            });
        },
        imgUploadSuccess(response, file, fileList) {
            console.log(response);
            if (response.code == 200) {
                this.dataForm.image = response.data;
                console.log("this.article", this.article);
            } else {
                this.$message.warning(response.msg);
            }
        },
        cancal(){
            this.visible = false
            this.$emit("refreshDataList");
            this.$emit('hide')
        },
        handleAvatarSuccess (res) {
            console.log(res)
            this.photo = res.path
            this.dataForm.image = res.path
            this.$message.success(res.msg)
            //this.$store.commit('user/updatePhoto', res.path)
        },
        beforeAvatarUpload (file) {
            //const isJPG = file.type.indexOf('image/') >= 0
            const isLt2M = file.size / 1024 / 1024 < 10

            /*if (!isJPG) {
                this.$message.error('上传头像只能是图片格式!')
                return false
            }*/
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 10MB!')
                return false
            }
            return true
        }

    }
};
</script>
